package com.wmh.baseservice.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wmh.baseservice.admin.entity.AdMenu;
import com.wmh.baseservice.admin.pojo.vo.menu.AddV2ReqVo;
import com.wmh.baseservice.admin.pojo.vo.menu.ListQueryReqVO;
import com.wmh.baseservice.admin.pojo.vo.menu.UpdateV2ReqVO;
import com.wmh.baseservice.admin.service.AdMenuService;
import com.wmh.baseservice.admin.service.AdRoleMenuService;
import com.wmh.baseservice.admin.tools.enums.AdMenuFlag;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mmx generator
 * @since 2021-02-03
 */
@Api(tags = "管理员-菜单")
@RestController
@RequestMapping("superAdmin/adMenu")
@Validated
public class AdMenuController {

    @Resource
    private AdMenuService adMenuService;

    @Resource
    private AdRoleMenuService adRoleMenuService;


    @ApiOperation("获取指定角色的菜单(按 Map 返回)")
    @PostMapping("getMenuByRoleId")
    public String getMenuByRoleId(@NotNull(message = "请上传角色编号") Long roleId){
        Map<Integer, List<AdMenu>> map = adMenuService.getMenuMapping(roleId);
        return ReturnBody.success(map);
    }

    @ApiOperation("所有菜单")
    @PostMapping("list")
    @RequiresPermissions({"menu.query"})
    public String list(ListQueryReqVO reqVO) {
        LambdaQueryWrapper<AdMenu> queryWrapper = new LambdaQueryWrapper<AdMenu>()
                .like(StrUtil.isNotBlank(reqVO.getMenuName()),AdMenu::getMenuName,reqVO.getMenuName())
                .orderByDesc(AdMenu::getSort);
        List<AdMenu> list = adMenuService.list(queryWrapper);
        return ReturnBody.success(list);
    }

    @ApiOperation("获取子菜单列表")
    @PostMapping("subList")
    public String subList(@NotNull(message = "请上传 pId") Long pId) {
        return ReturnBody.success(adMenuService.subList(pId));
    }

    @ApiOperation("菜单分页列表")
    @PostMapping("pages")
    @RequiresPermissions({"menu.query"})
    public String pages(@Valid PageInfo pageInfo, Integer menusType) {
        PageResult<AdMenu> pageResult = new PageUtil<AdMenu>(pageInfo).startPage((page, queryWrapper) -> {
            if (menusType != null){
                queryWrapper.eq(AdMenu.FLAG,menusType);
            }
            queryWrapper.orderByDesc(AdMenu.CREATE_TIME);
            adMenuService.page(page, queryWrapper);
        });
        return ReturnBody.success(pageResult);
    }


    @ApiOperation("菜单权限")
    @PostMapping("options")
    public String options() {
        List<AdMenu> list = adMenuService.list(new QueryWrapper<AdMenu>().select(AdMenu.ID, AdMenu.MENU_NAME, AdMenu.FLAG));
        HashMap<Integer, List<AdMenu>> hashMap = new HashMap<>();
        hashMap.put(AdMenuFlag.MENU.getValue(),new ArrayList<>());
        hashMap.put(AdMenuFlag.INTERFACE.getValue(),new ArrayList<>());
        for (AdMenu menu : list) {
            List<AdMenu> adMenus = hashMap.get(menu.getFlag());
            adMenus.add(menu);
        }
        return ReturnBody.success(hashMap);
    }

    @ApiOperation("树级菜单")
    @PostMapping("tree")
    public String tree() {
        return ReturnBody.success(adMenuService.treeList());
    }

    @ApiOperation("添加菜单v2")
    @PostMapping("addV2")
    @RequiresPermissions({"menu.create"})
    public String addV2(@Valid AddV2ReqVo reqVo) {
        adMenuService.addV2(reqVo);
        return ReturnBody.success();
    }

    @ApiOperation("删除菜单V2")
    @PostMapping("deleteV2")
    @RequiresPermissions({"menu.delete"})
    public String deleteV2(Long id) {
        adMenuService.deleteV2(id);
        return ReturnBody.success();
    }

    @ApiOperation("修改菜单V2")
    @PostMapping("updateV2")
    @RequiresPermissions({"menu.update"})
    public String updateV2(@Valid UpdateV2ReqVO reqVO){
        adMenuService.updateV2(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation(value = "获取指定角色的菜单编号列表")
    @GetMapping("idsByRoleId")
    public String idsByRoleId(@NotNull(message = "未提交角色编号") Long roleId){
        return ReturnBody.success(adRoleMenuService.getMenuIds(roleId));
    }

    @ApiOperation("根据管理员编号获取菜单列表")
    @GetMapping("listMenuByAdId")
    public String listMenuByAdId(Long adId){
        return ReturnBody.success(adMenuService.getAdMenuByCache(adId,AdMenuFlag.MENU));
    }
}
